package com.dq.ad;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HxCommonUtil {
    private static final String TAG = "HxCommonUtil";

    public static String getInstallAppList(Context context) {
        List<AppBean> appBeanList = new ArrayList<>();
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : packages) {
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            // 非系统应用
            if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                String appName = applicationInfo.loadLabel(pm).toString();
                String packageName = packageInfo.packageName;
                if (!packageName.equals(context.getPackageName())) {
                    appBeanList.add(new AppBean(appName, packageName));
//                    Log.e(TAG, "packageName: " + packageName + " , appName: " + appName);
                }
            }
        }

        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONObject subObject;

        for (AppBean appBean : appBeanList) {
            subObject = new JSONObject();
            try {
                subObject.put("appName", appBean.appName);
                subObject.put("packageName", appBean.packageName);
                jsonArray.put(subObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        try {
            jsonObject.put("appList", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonObject.toString();
    }

    public static String getRunningAppList(Context context) {
        List<AppBean> appBeanList = new ArrayList<>();
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : packages) {
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            // 非系统应用
            if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                if ((applicationInfo.flags & ApplicationInfo.FLAG_STOPPED) == 0) {
                    String appName = applicationInfo.loadLabel(pm).toString();
                    String packageName = packageInfo.packageName;
                    if (!packageName.equals(context.getPackageName())) {
                        appBeanList.add(new AppBean(appName, packageName));
//                        Log.e(TAG, "packageName: " + packageName + " , appName: " + appName);
                    }
                }
            }
        }

        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONObject subObject;

        for (AppBean appBean : appBeanList) {
            subObject = new JSONObject();
            try {
                subObject.put("appName", appBean.appName);
                subObject.put("packageName", appBean.packageName);
                jsonArray.put(subObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        try {
            jsonObject.put("appList", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonObject.toString();
    }

    private static class AppBean {
        private String appName;
        private String packageName;

        public String getAppName() {
            return appName;
        }

        public void setAppName(String appName) {
            this.appName = appName;
        }

        public String getPackageName() {
            return packageName;
        }

        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }

        public AppBean(String appName, String packageName) {
            this.appName = appName;
            this.packageName = packageName;
        }

        @Override
        public String toString() {
            return "AppBean{" +
                    "appName='" + appName + '\'' +
                    ", packageName='" + packageName + '\'' +
                    '}';
        }
    }
}
